package io.kiki.sba.registry.common.model.slot;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;
import java.util.Collection;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

@Setter
@Getter
public final class Slot implements Serializable, Cloneable {
    private final int id;
    private final String leaderDataStoreNodeId;
    private final long leaderEpoch;
    private final Set<String> followerDataStoreNodeIdSet;

    @JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
    public Slot(@JsonProperty("id") int id, @JsonProperty("leader") String leaderDataStoreNodeId, @JsonProperty("leaderEpoch") long leaderEpoch, @JsonProperty("followers") Collection<String> followerDataStoreNodeIdSet) {
        this.id = id;
        this.leaderDataStoreNodeId = leaderDataStoreNodeId;
        this.leaderEpoch = leaderEpoch;
        if (followerDataStoreNodeIdSet == null) {
            this.followerDataStoreNodeIdSet = new HashSet<>();
        } else {
            this.followerDataStoreNodeIdSet = new HashSet<>(followerDataStoreNodeIdSet);
        }
    }

    @Override
    protected Slot clone() {
        return new Slot(this.id, this.leaderDataStoreNodeId, this.leaderEpoch, this.followerDataStoreNodeIdSet);
    }


    @Override
    public boolean equals(Object o) {
        if (this == o)
            return true;
        if (!(o instanceof Slot))
            return false;
        Slot slot = (Slot) o;
        return id == slot.id && leaderEpoch == slot.leaderEpoch && Objects.equals(leaderDataStoreNodeId, slot.leaderDataStoreNodeId) && Objects.equals(followerDataStoreNodeIdSet, slot.followerDataStoreNodeIdSet);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, leaderDataStoreNodeId, leaderEpoch, followerDataStoreNodeIdSet);
    }

}
